<?php
/**
 * Handles dashboard functions for the website
 *
 * @package default
 * @author Jimmy E. Lewis
 **/
class Dashboard extends Controller{
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->load->model('UserModel');
	}
	
	
	function index(){
		$this->load->library('Dashboard');
		
		$this->Dashboard->showBoard();
	}
	
	/**
	 * Displays all prayers
	 */
	function prayers(){
		$this->badger->checkAllow('admin lg_pop');
		
		$data = $this->_buildContent();
		$this->load->model('ContentModel');
		
		$prayers = $this->ContentModel->getPrayers();
		
		$data['prayers'] = $prayers;
		$data['extraHead'] = '<link rel="stylesheet" href="'.base_url().'css/thickbox.css" type="text/css" media="screen" />'.PHP_EOL.
		'<script src="'.site_url("js/thickbox-compressed.js").'" type="text/javascript"></script>';
		$this->load->view('dashboard/prayer/prayers', $data);
		
	}
	
	/**
	 * Displays a single prayer
	 */
	function prayer(){
		$this->badger->checkAllow('admin lg_pop');
		
		$this->load->model('ContentModel');
		
		$prayer = $this->ContentModel->getPrayer($this->uri->segment(3));

		if($prayer != null){
			$data['prayer'] = $prayer;
			$this->load->view('dashboard/prayer/prayer', $data);
		}
		else{
			$this->session->set_flashdata('msg_error', "This prayer request doesn't exist."); 
			redirect('dashboard/prayers');
		}
	}
	
	function prayer_reply(){
		$this->badger->checkAllow('admin lg_pop');
		
		$this->load->model('ContentModel');
		
		$prayer = $this->ContentModel->getPrayer($this->uri->segment(3));
		
		if($prayer != null){
			$this->load->library('form_validation');
		
			$config = array(
			               array(
								'field'	   => 'reply',
								'label'    => 'Reply',
								'rules'    => 'trim|required|xss_clean'
								)
							);
		
			$this->form_validation->set_rules($config);
		
			$this->form_validation->set_error_delimiters('<div class="error">','</div>');
		
			if($this->form_validation->run() == FALSE) {
				$data['prayer'] = $prayer;
				$this->load->view('dashboard/prayer/reply',$data);
			} else {
				$this->session->set_flashdata('msg_info', "Your reply has been sent"); 
				$user = $this->UserModel->getUserByName($this->session->userdata('username'));
				
				$post['reply'] = $this->input->post('reply',TRUE);
				$post['prayer_id'] = $this->uri->segment(3);
				$post['userid'] = $user->userid;
				
				$this->load->model('ContentModel');
				$this->ContentModel->createPrayerReply($post);
				
				//send email to person who requested prayer
				$this->load->library('email');
				$this->email->from('donotreply@xa-ncsu.com','Chi Alpha Christian Fellowship');
				$this->email->to("$prayer->email");
				$this->email->subject('Reply to your prayer request.');
				$this->email->message('The following reply was submit for your prayer\n' . $post['reply']);

				$this->email->send();
				
				redirect('dashboard/prayers');
			}
		}
		else{
			$this->session->set_flashdata('msg_error', "Can't reply to a request that doesn't exist."); 
			redirect('dashboard/prayers');
		}
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 * REQUIRED DATA ARRAY PARAMS:
	 * pagetype = page, page-admin, nobg
	 * loggedin = isset
	 */
	function _buildContent() {
		$data = array();
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error'))	$data['msg_error'] = $this->session->flashdata('msg_error');
		
		if ($this->session->userdata('loggedin')) {
			$this->load->model('UserModel');
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			$data['con_user'] = $user;
			$data['loggedin'] = TRUE;
			
			//is admin?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('admin'))) {
				$data['admin_tab'] = TRUE;
			}
			
			//is prayer team?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
				$data['prayer_tab'] = TRUE;
			}
		}
		
		return $data;
	}
	
}






?>